<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px"
    >
      <el-form-item label="应用信息" prop="app_id">
        <el-select
          v-model="queryParams.app_id"
          placeholder="请选择应用信息"
        >
          <el-option
            v-for="item in appOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户编号" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入用户编号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入用户名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="精确用户名称" prop="exact_name">
        <el-input
          v-model="queryParams.exact_name"
          placeholder="请输入精确用户名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邀请码" prop="referral_code">
        <el-input
          v-model="queryParams.referral_code"
          placeholder="请输入邀请码"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册ip" prop="register_ip">
        <el-input
          v-model="queryParams.register_ip"
          placeholder="请输入注册ip"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册地区" prop="register_address">
        <el-input
          v-model="queryParams.register_address"
          placeholder="请输入注册地区"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上级代理" prop="agent_name">
        <el-input
          v-model="queryParams.agent_name"
          placeholder="请输入上级代理"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="精确上级代理" prop="exact_agent_name">
        <el-input
          v-model="queryParams.exact_agent_name"
          placeholder="请输精确入上级代理"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="层级" prop="level">
        <el-input
          v-model="queryParams.level"
          placeholder="请输入层级"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最低总活期账户" prop="min_total_amount">
        <el-input
          type="number" step="0.01"
          v-model="queryParams.min_total_amount"
          placeholder="请输入最低总活期账户"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最高总活期账户" prop="max_total_amount">
        <el-input
          type="number" step="0.01"
          v-model="queryParams.max_total_amount"
          placeholder="请输入最高总活期账户"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="用户状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="ip状态" prop="ip_status">
        <el-select
          v-model="queryParams.ip_status"
          placeholder="ip状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.customer_ip_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="注册时间">
        <el-date-picker
          v-model="registerTimeRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="团队统计时间月份">
        <el-date-picker
          v-model="queryParams.team_statics_time"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          format="yyyy-MM-dd HH:mm:ss"
          type="month"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary" icon="el-icon-search" size="mini"
          v-hasPermi="['customer:user:query']" @click="handleQuery"
        >
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @sort-change="sortChange">
      <el-table-column sortable="custom" label="用户编号" align="center" key="id" prop="id"
                       v-if="showColumns.id"/>
      <el-table-column label="用户名称" align="center" key="name" prop="name"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.name"
      />
      <el-table-column
        label="报表"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
        v-if="showColumns.report"
      >
        <template slot-scope="scope">
          <router-link :to="'/customer/deposit-record/index/' + queryParams.app_id + '/' + scope.row.name"
                       class="link-type">
            <el-button size="mini" type="text" icon="el-icon-search">充值</el-button>
          </router-link>
          <router-link :to="'/customer/withdraw-record/index/' + queryParams.app_id + '/' + scope.row.name"
                       class="link-type">
            <el-button size="mini" type="text" icon="el-icon-search">提现</el-button>
          </router-link>
          <router-link :to="'/customer/account-record/index/' + queryParams.app_id + '/' + scope.row.name"
                       class="link-type">
            <el-button size="mini" type="text" icon="el-icon-search">账户</el-button>
          </router-link>
          <router-link :to="'/customer/project-record/index/' + queryParams.app_id + '/' + scope.row.name"
                       class="link-type">
            <el-button size="mini" type="text" icon="el-icon-search">项目</el-button>
          </router-link>
          <router-link :to="'/customer/quantification-record/index/' + queryParams.app_id + '/' + scope.row.name"
                       class="link-type">
            <el-button size="mini" type="text" icon="el-icon-search">量化</el-button>
          </router-link>
          <router-link :to="'/customer/activity-record/index/' + queryParams.app_id + '/' + scope.row.name"
                       class="link-type">
            <el-button size="mini" type="text" icon="el-icon-search">活动</el-button>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
        v-if="showColumns.operator"
      >
        <template slot-scope="scope" v-if="scope.row.id !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="openSelectRegisterIpUser(scope.row)"
            v-hasPermi="['customer:user:query']"
          >稽查注册IP
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['customer:user:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdateAppQuantificationId(scope.row)"
            v-hasPermi="['customer:user:edit']"
          >修改量化
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleExchange(scope.row)"
            v-hasPermi="['customer:account:exchange']"
          >转换账户
          </el-button>
          <router-link :to="'/customer/record/index/' + queryParams.app_id + '/agent/' + scope.row.name"
                       class="link-type">
            <el-button size="mini" type="text" icon="el-icon-search">下级</el-button>
          </router-link>
          <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)">
                <span class="el-dropdown-link">
                  <i class="el-icon-d-arrow-right el-icon--right"></i>更多
                </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="handleResetPwd" icon="el-icon-key"
                                v-hasPermi="['customer:user:resetPwd']"
              >重置密码
              </el-dropdown-item>
              <el-dropdown-item command="handleResetTradePwd" icon="el-icon-key"
                                v-hasPermi="['customer:user:resetTradePwd']"
              >重置交易密码
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
      <el-table-column sortable="custom" label="活期账户" align="center" key="total_amount" prop="total_amount"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.total_amount"
      />
      <el-table-column sortable="custom" label="智能账户" align="center" key="total_ai_amount" prop="total_ai_amount"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.total_ai_amount"
      />
      <el-table-column sortable="custom" label="所在层级" align="center" key="level" prop="level"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.level"
      >
        <template slot-scope="scope">
          <span style="color: #006ce7" @click="handleLevel(scope.row)">{{ scope.row.level }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable="custom" label="充提差" align="center" key="total_deposit_withdraw_differ"
                       prop="total_deposit_withdraw_differ"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.total_deposit_withdraw_differ"
      />
      <el-table-column sortable="custom" label="团队充值" align="center" key="team_deposit" prop="team_deposit"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.team_deposit"
      >
        <template slot-scope="scope">
          <router-link :to="'/customer/team-deposit/index/' + scope.row.id" class="link-type">
            <span>{{ scope.row.team_deposit }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column sortable="custom" label="团队提现" align="center" key="team_withdraw" prop="team_withdraw"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.team_withdraw"
      />
      <el-table-column sortable="custom" label="团队充提差" align="center" key="team_deposit_withdraw_differ"
                       prop="team_deposit_withdraw_differ"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.team_deposit_withdraw_differ"
      />
      <el-table-column label="月份团队充值" align="center" key="month_team_deposit_amount"
                       prop="month_team_deposit_amount"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.month_team_deposit_amount"
      />
      <el-table-column label="月份团队有效会员" align="center" key="month_team_number" prop="month_team_number"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.month_team_number"
      />
      <el-table-column label="上级代理" align="center" key="agent_name" prop="agent_name"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.agent_name"
      />
      <el-table-column label="一级代理" align="center" key="first_agent_name" prop="first_agent_name"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.first_agent_name"
      />
      <el-table-column label="注册地区" align="center" key="register_address" prop="register_address"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.register_address"
      />
      <el-table-column label="手机号" align="center" key="phone" prop="phone"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.phone"
      />
      <el-table-column label="能量金额(trx)" align="center" key="total_energy_trx_amount"
                       prop="total_energy_trx_amount"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.total_energy_trx_amount"
      />
      <el-table-column label="ip状态" width="80" prop="ip_status" v-if="showColumns.ip_status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.customer_ip_status" :value="scope.row.ip_status"/>
        </template>
      </el-table-column>
      <el-table-column label="注册ip" align="center" key="register_ip" prop="register_ip"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.register_ip"
      />
      <el-table-column label="登录ip" align="center" key="login_ip" prop="login_ip"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.login_ip"
      />
      <el-table-column label="level1" align="center" key="level1" prop="level1"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.level1"
      />
      <el-table-column label="level2" align="center" key="level2" prop="level2"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.level2"
      />
      <el-table-column label="level3" align="center" key="level3" prop="level3"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.level3"
      />
      <el-table-column prop="status" label="状态" width="80" v-if="showColumns.status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column prop="quantification_status" label="量化状态" width="80"
                       v-if="showColumns.quantification_status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.quantification_status"/>
        </template>
      </el-table-column>
      <el-table-column prop="withdraw_status" label="提现状态" width="80" v-if="showColumns.withdraw_status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.withdraw_status"/>
        </template>
      </el-table-column>
      <el-table-column label="注册时间" align="center" key="create_time" prop="create_time" width="160"
                       v-if="showColumns.create_time"/>
      <el-table-column label="邀请码" align="center" key="referral_code" prop="referral_code"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.referral_code"
      />
      <el-table-column label="邮箱" align="center" key="email" prop="email"
                       :show-overflow-tooltip="true" width="160" v-if="showColumns.email"
      />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.page_size"
      @pagination="getList"
    />

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-form-item label="应用信息" prop="app_id">
            <el-select
              v-model="form.app_id"
              placeholder="请选择应用信息"
              :disabled="form.id != undefined"
            >
              <el-option
                v-for="item in appOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="用户名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入用户名称" maxlength="30" :disabled="form.id != undefined"/>
          </el-form-item>
          <el-form-item label="昵称" prop="nickname">
            <el-input v-model="form.nickname" placeholder="请输入昵称" maxlength="30"/>
          </el-form-item>
          <el-form-item label="用户密码" prop="password" v-if="form.id == undefined">
            <el-input v-model="form.password" placeholder="请输入用户密码" maxlength="30"/>
          </el-form-item>
          <el-form-item label="用户交易密码" prop="trade_password" v-if="form.id == undefined">
            <el-input v-model="form.trade_password" placeholder="请输入用户交易密码" maxlength="30"/>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="form.phone" placeholder="请输入手机号" maxlength="30"/>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio
                v-for="dict in dict.type.sys_normal_disable"
                :key="dict.value"
                :label="parseInt(dict.value)"
              >{{ dict.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="ip状态" prop="ip_status">
            <el-radio-group v-model="form.ip_status">
              <el-radio
                v-for="dict in dict.type.customer_ip_status"
                :key="dict.value"
                :label="parseInt(dict.value)"
              >{{ dict.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="量化状态" prop="quantification_status" v-if="form.id !== undefined">
            <el-radio-group v-model="form.quantification_status">
              <el-radio
                v-for="dict in dict.type.sys_normal_disable"
                :key="dict.value"
                :label="parseInt(dict.value)"
              >{{ dict.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="提现状态" prop="withdraw_status" v-if="form.id !== undefined">
            <el-radio-group v-model="form.withdraw_status">
              <el-radio
                v-for="dict in dict.type.sys_normal_disable"
                :key="dict.value"
                :label="parseInt(dict.value)"
              >{{ dict.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="resetTitle" :visible.sync="resetPwdOpen" width="600px" append-to-body
               :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-form-item label="新密码" prop="password">
            <el-input v-model="form.password" placeholder="请输入新密码" maxlength="30"/>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="resetPwd">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="resetTradeTitle" :visible.sync="resetTradePwdOpen" width="600px" append-to-body
               :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-form-item label="新交易密码" prop="password">
            <el-input v-model="form.password" placeholder="请输入新密码" maxlength="30"/>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="resetTradePwd">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="exchangeTitle" :visible.sync="exchangeOpen" width="800px" append-to-body
               :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-form-item label="类型" prop="type">
            <el-radio-group v-model="form.type">
              <el-radio
                v-for="dict in dict.type.customer_account_exchange"
                :key="dict.value"
                :label="parseInt(dict.value)"
              >{{ dict.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="金额" prop="amount">
            <el-input type="number" v-model="form.amount" maxlength="30"/>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="exchange">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="appQuantificationTitle" :visible.sync="appQuantificationOpen" width="600px" append-to-body
               :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-form-item label="应用信息" prop="app_id">
            <el-select
              v-model="form.app_id"
              placeholder="请选择应用信息"
              @change="changeAppQuantification"
            >
              <el-option
                v-for="item in appOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="量化信息" prop="app_quantification_id">
            <el-select
              v-model="form.app_quantification_id"
              placeholder="请选择量化信息"
              clearable
              @change="$forceUpdate()"
            >
              <el-option
                v-for="item in appQuantificationOptions"
                :key="parseInt(item.id)"
                :label="item.title"
                :value="parseInt(item.id)"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="利润率(%)" prop="profit_rate">
            <el-input type="number" step="0.01" v-model="form.profit_rate"
                      placeholder="请输入利润率(%)" @input="$forceUpdate()"></el-input>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormAppQuantificationId">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="levelTitle" :visible.sync="levelOpen" width="600px" append-to-body>
      <li v-for="level_customer in level_customer_list">
        <button @click="queryName(level_customer.name)">查看信息</button>
        用户名:{{ level_customer.name }} 所在层级: {{ level_customer.level }}
      </li>
    </el-dialog>

    <select-register-ip-customer ref="select"
                                 :app_id="queryParams.app_id"
                                 @ok="handleQuery"
    />
  </div>
</template>

<script>
import {
  getUser,
  listUserV5,
  resetTradePwd,
  resetUserPwd,
  updateUser,
  updateUserAppQuantificationId
} from '@/api/customer/customer'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import {getGroupApp} from "@/api/metadata/app";
import selectRegisterIpCustomer from "@/views/customer/user/selectRegisterIpCustomer";
import {exchange} from "@/api/customer/customerAccount";
import {getListAppQuantification} from "@/api/metadata/appQuantification";

export default {
  name: 'CustomerUser',
  dicts: ['sys_normal_disable', 'app_flag', 'customer_ip_status', 'customer_account_exchange'],
  components: {selectRegisterIpCustomer},
  data() {
    return {
      // 弹出层标题
      exchangeTitle: '',
      // 是否显示弹出层
      exchangeOpen: false,
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 弹出层标题
      title: '',
      appQuantificationTitle: '',
      // 弹出层标题
      resetTitle: '',
      resetTradeTitle: '',
      levelTitle: '',
      // 是否显示弹出层
      open: false,
      appQuantificationOpen: false,
      // 是否显示弹出层
      resetPwdOpen: false,
      resetTradePwdOpen: false,
      levelOpen: false,
      admin: false,
      level_customer_list: [],
      // 日期范围
      registerTimeRange: [],
      appOptions: [],
      appQuantificationOptions: [],
      // 表单参数
      form: {},
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 查询参数
      queryParams: {
        page: 1,
        page_size: 10,
        app_id: undefined,
        id: undefined,
        name: undefined,
        exact_name: undefined,
        phone: undefined,
        agent_name: undefined,
        exact_agent_name: undefined,
        referral_code: undefined,
        register_ip: undefined,
        register_address: undefined,
        status: undefined,
        ip_status: undefined,
        sort_list: [],
        level: undefined,
        min_total_amount: undefined,
        max_total_amount: undefined,
        team_statics_time: undefined
      },
      selectRegisterIpCustomer: {
        register_ip: undefined
      },
      // 表单校验
      rules: {
        app_id: [
          {required: true, message: '应用信息不能为空', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '用户密码不能为空', trigger: 'blur'},
          {min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur'}
        ],
        trade_password: [
          {required: true, message: '用户交易密码不能为空', trigger: 'blur'},
          {min: 5, max: 20, message: '用户交易密码长度必须介于 5 和 20 之间', trigger: 'blur'}
        ],
        google_code: [
          {required: true, message: 'google验证码不能为空', trigger: 'blur'},
        ]
      },
      columns: [
        {key: 1, name: 'id', label: `用户编号`, visible: true},
        {key: 2, name: 'name', label: `用户名称`, visible: true},
        {key: 3, name: 'report', label: `报表`, visible: true},
        {key: 4, name: 'operator', label: `操作`, visible: true},
        {key: 5, name: 'total_amount', label: `活期账户`, visible: true},
        {key: 6, name: 'total_ai_amount', label: `智能账户`, visible: true},
        {key: 7, name: 'level', label: `所在层级`, visible: true},
        {key: 8, name: 'total_deposit_withdraw_differ', label: `充提差`, visible: true},
        {key: 9, name: 'team_deposit', label: `团队充值`, visible: true},
        {key: 10, name: 'team_withdraw', label: `团队提现`, visible: true},
        {key: 11, name: 'team_deposit_withdraw_differ', label: `团队充提差`, visible: true},
        {key: 12, name: 'month_team_deposit_amount', label: `月份团队充值`, visible: true},
        {key: 13, name: 'month_team_number', label: `月份团队有效会员`, visible: true},
        {key: 14, name: 'agent_name', label: `上级代理`, visible: true},
        {key: 15, name: 'first_agent_name', label: `一级代理`, visible: true},
        {key: 16, name: 'register_address', label: `注册地区`, visible: true},
        {key: 17, name: 'phone', label: `手机号`, visible: true},
        {key: 18, name: 'total_energy_trx_amount', label: `能量金额(trx)`, visible: false},
        {key: 19, name: 'ip_status', label: `ip状态`, visible: false},
        {key: 20, name: 'register_ip', label: `注册ip`, visible: false},
        {key: 21, name: 'login_ip', label: `登录ip`, visible: false},
        {key: 22, name: 'level1', label: `level1`, visible: false},
        {key: 23, name: 'level2', label: `level2`, visible: false},
        {key: 24, name: 'level3', label: `level3`, visible: false},
        {key: 25, name: 'status', label: `状态`, visible: false},
        {key: 26, name: 'quantification_status', label: `量化状态`, visible: false},
        {key: 27, name: 'withdraw_status', label: `提现状态`, visible: false},
        {key: 28, name: 'create_time', label: `注册时间`, visible: false},
        {key: 29, name: 'referral_code', label: `邀请码`, visible: false},
        {key: 30, name: 'email', label: `邮箱`, visible: false},
      ],
    }
  },
  computed: {
    showColumns: function () {
      window.localStorage.setItem('CustomerUserColumns-5', JSON.stringify(this.columns));
      let newShowColumns = {}
      for (let i = 0; i < this.columns.length; i++) {
        let item = this.columns[i];
        Object.defineProperty(newShowColumns, item.name, {value: item.visible});
      }
      return newShowColumns;
    }
  },
  created() {
    const exactName = this.$route.params && this.$route.params.exactName;
    if (exactName) {
      this.queryParams.exact_name = exactName
    }
    const exactAgentName = this.$route.params && this.$route.params.exactAgentName;
    if (exactAgentName) {
      this.queryParams.exact_agent_name = exactAgentName
    }
    // 默认id排序
    this.queryParams.sort_list = [{sort_by: 'id', sort_type: 2}]
    const appId = this.$route.params && this.$route.params.appId;
    this.getGroupApp(appId)
    if (window.localStorage.getItem('CustomerUserColumns-5')) {
      this.columns = JSON.parse(window.localStorage.getItem('CustomerUserColumns-5'))
    }
  },
  methods: {
    sortChange(column) {
      if (column) {
        if (column.order === 'ascending') {
          this.queryParams.sort_list = [{sort_by: column.prop, sort_type: 1}]
        } else if (column.order === 'descending') {
          this.queryParams.sort_list = [{sort_by: column.prop, sort_type: 2}]
        }
        this.getList()
      }
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true
      listUserV5(this.addDateRange(this.queryParams, this.registerTimeRange, '_register_time')).then(response => {
          this.userList = response.data
          this.total = response.total
          this.loading = false
        }
      )
    },
    getGroupApp(appId) {
      getGroupApp().then(response => {
        this.queryParams.type = 1
        this.appOptions = response.data
        if (appId) {
          this.queryParams.app_id = parseInt(appId)
        } else {
          this.queryParams.app_id = response.data[0].id
        }
        this.getList()
      })
    },
    changeAppQuantification(val) {
      getListAppQuantification(val).then(response => {
        this.appQuantificationOptions = response.data
        this.form.app_quantification_id = undefined
      })
    },
    changeAppQuantification2(val) {
      getListAppQuantification(val).then(response => {
        this.appQuantificationOptions = response.data
      })
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.resetPwdOpen = false
      this.exchangeOpen = false
      this.appQuantificationOpen = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        nickname: undefined,
        name: undefined,
        password: undefined,
        trade_password: undefined,
        phone: undefined,
        email: undefined,
        google_code: '',
        status: 1,
        quantification_status: 1,
        amount: 0.00,
        customer_id: undefined,
        type: undefined,
        app_quantification_id: undefined,
        profit_rate: undefined,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.createTimeRange = []
      let appId = this.queryParams.app_id
      this.resetForm('queryForm')
      this.queryParams.app_id = appId
      this.handleQuery()
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case 'handleResetPwd':
          this.handleResetPwd(row)
          break;
        case 'handleResetTradePwd':
          this.handleResetTradePwd(row)
          break;
        case "handleAuthRole":
          this.handleAuthRole(row);
          break;
        default:
          break
      }
    },
    /** 分配角色操作 */
    handleAuthRole: function (row) {
      const id = row.id;
      this.$router.push("/tenant/user-auth/role/" + id);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      getUser(row.id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改用户'
      })
    },
    /** 修改按钮操作 */
    handleUpdateAppQuantificationId(row) {
      this.reset()
      getUser(row.id).then(response => {
        this.form = response.data
        this.form.app_quantification_id = row.app_quantification_id
        this.form.profit_rate = row.profit_rate
        this.changeAppQuantification2(this.form.app_id)
        this.appQuantificationOpen = true
        this.appQuantificationTitle = '修改用户量化'
      })
    },
    /** 重置密码按钮操作 */
    handleResetPwd(row) {
      this.reset()
      this.form.id = row.id
      this.resetPwdOpen = true
      this.resetTitle = '重置密码'
    },
    /** 重置交易密码按钮操作 */
    handleResetTradePwd(row) {
      this.reset()
      this.form.id = row.id
      this.resetTradePwdOpen = true
      this.resetTradeTitle = '重置交易密码'
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 提交按钮 */
    submitFormAppQuantificationId: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateUserAppQuantificationId(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.appQuantificationOpen = false
              this.getList()
            })
          }
        }
      })
    },
    /** 提交按钮 */
    resetPwd: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          resetUserPwd(this.form.id, this.form.password, this.form.google_code).then(response => {
            this.$modal.msgSuccess('重置密码成功')
            this.resetPwdOpen = false
            this.getList()
          })
        }
      })
    },
    /** 提交按钮 */
    resetTradePwd: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          resetTradePwd(this.form.id, this.form.password, this.form.google_code).then(response => {
            this.$modal.msgSuccess('重置交易密码成功')
            this.resetTradePwdOpen = false
            this.getList()
          })
        }
      })
    },
    /** 打开ip一样的用户表弹窗 */
    openSelectRegisterIpUser(row) {
      this.$refs.select.show(row.register_ip);
    },
    /** 转换账户 */
    handleExchange: function (row) {
      this.reset()
      this.form.customer_id = row.id
      this.form.type = 15
      this.exchangeOpen = true
      this.exchangeTitle = '转换账户'
    },
    /** 等级层级 */
    handleLevel: function (row) {
      this.levelOpen = true
      this.levelTitle = '所在层级'
      this.level_customer_list = row.level_customer_list
    },
    /** 转换账户 */
    queryExactAgentName: function (row) {
      this.createTimeRange = []
      let appId = this.queryParams.app_id
      this.resetForm('queryForm')
      this.queryParams.app_id = appId
      this.queryParams.exact_agent_name = row.name
      this.handleQuery()
    },
    /** 查看会员信息 */
    queryName: function (name) {
      this.levelOpen = false
      this.createTimeRange = []
      let appId = this.queryParams.app_id
      this.resetForm('queryForm')
      this.queryParams.app_id = appId
      this.queryParams.name = name
      this.handleQuery()
    },
    /** 提交按钮 */
    exchange: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          exchange(this.form).then(response => {
            this.$modal.msgSuccess('转换金额成功')
            this.exchangeOpen = false
            this.getList()
          })
        }
      })
    },
  }
}
</script>
